package com.uex.dev.manage.admin.controller;

import com.google.common.base.Preconditions;
import com.uex.dev.manage.admin.po.UserPO;
import com.uex.dev.manage.admin.service.IUserService;
import com.uex.dev.manage.admin.vo.user.UserPageReq;
import com.uex.dev.manage.admin.vo.user.UserPageResp;
import com.uex.dev.manage.common.base.ModelResult;
import com.uex.dev.manage.common.base.PageResult;
import io.micrometer.common.util.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author lzs
 * @create 2024/02/22
 * @description
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Tag(name = "用户管理")
@RequestMapping("/user")
@RestController
public class UserController {

    @Resource
    private IUserService userService;

    @Operation(description = "新增用户")
    @PostMapping("/add")
    public ModelResult<?> add(@RequestBody UserPO user) {
        Preconditions.checkArgument(StringUtils.isNotBlank(user.getUserName()), "用户名不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(user.getUserAccount()), "账号不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(user.getUserPasswd()), "密码不能为空");
        Preconditions.checkArgument(Objects.nonNull(user.getUserType()), "用户类型不能为空");
        return userService.add(user) ? ModelResult.SUCCESS_RESULT : ModelResult.SYSTEM_ERROR_RESULT;
    }

    @Operation(description = "用户登录")
    @PostMapping("/login")
    public ModelResult<String> login(@RequestBody UserPO user) {
        Preconditions.checkArgument(StringUtils.isNotBlank(user.getUserAccount()), "账号不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(user.getUserPasswd()), "密码不能为空");
        return new ModelResult(userService.login(user));
    }

    @Operation(description = "获取用户列表")
    @PostMapping("/list")
    public ModelResult<PageResult<UserPageResp>> pageUser(@RequestBody UserPageReq userPageReq) {
        return new ModelResult<>(userService.selectList(userPageReq));
    }

    @Operation(description = "删除用户")
    @GetMapping("/del")
    public ModelResult del(@RequestParam Integer id) {
        userService.removeById(id);
        return ModelResult.SUCCESS_RESULT;
    }
}

